package com.wu.server.service;

import com.wu.server.code.Code;
import com.wu.server.config.EncodeJson;
import com.wu.server.dao.GoodsRepository;
import com.wu.server.dao.GoodsTagRepository;
import com.wu.server.entity.Goods;
import com.wu.server.entity.Tag;
import com.wu.server.websocket.GoodsTagWebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.websocket.EncodeException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * // TODO: 2021/3/6 获取所有商品，获取商品中所有,号分割的标签名称存储到tag表中，读取tag表无重复字段查询goods表商品列表，读取商品列表中所有的sales总和
 * @author Wugs
 * @version 1.0
 * @date 2021/3/4 22:12
 */
@Service
public class GoodsTagServer {
    @Autowired
    GoodsRepository goodsRepository;
    @Autowired
    GoodsTagRepository goodsTagRepository;
    @Autowired
    EncodeJson encodeJson;
    @Autowired
    Code code;
    @Async
    public void execute(){
        goodsTagRepository.deleteAll();
        Iterable<Goods> goods_list =goodsRepository.findAll();
        getTagName(goods_list);
        this.sendData();
    }
    public void sendData(){
        Iterable<Tag> tag_list = goodsTagRepository.getTagInfo();
        HashMap<String,Object> result = new HashMap<>();
        List<Object> data = new ArrayList<>();
        for (Tag tag: tag_list){
            getTagInfo(data,tag);
        }
        HashMap<String,Object> meta = new HashMap<>();
        meta.put("code", code.TAGSALES);
        meta.put("msg",code.getMsg(code.TAGSALES));
        result.put("meta",meta);
        result.put("data",data);
        try {
            GoodsTagWebSocket.sendMessage(encodeJson.encode(result));
        } catch (EncodeException e) {
            e.printStackTrace();
        }
    }
    private void  getTagInfo(List<Object> data,Tag tag){
        HashMap<String,Object> tagInfo = new HashMap<>();
        tagInfo.put("name",tag.getTag_name());
        tagInfo.put("value",tag.getTag_summary());
        data.add(tagInfo);
    }
    /**
     * 获取商品信息数据进行tag数据分割
     * @param goods_list
     */
    private void getTagName(Iterable<Goods> goods_list){
        for (Goods goods:goods_list){
            String tag_name = goods.getTag_name();
            if(tag_name == null) continue;
            String[] str = tag_name.split(",");
            saveTag(str);
        }
    }

    /**
     * 存取字段到数据库
     * @param str
     */
    private void saveTag(String[] str){
        for (String tag_name:str){
            if(tag_name.equals("")){
            }else{
                if(goodsTagRepository.getTagNum(tag_name)==0){
                    Tag tag = new Tag();
                    tag.setTag_name(tag_name);
                    String tag_name_temp = "%"+tag_name+"%";
                    tag.setTag_summary(goodsRepository.getTagCount(tag_name_temp));
                    goodsTagRepository.save(tag);
                }
            }
        }
    }

    /**
     * 获取商品sales总和
     * @param sales_num
     * @return
     */
    private Integer getSalesNum(Iterable<Integer> sales_num){
        int sales = 0;
        for (Integer sales_temp : sales_num){
            sales+=sales_temp;
        }
        return sales;
    }
}
